Imports System.Data.SqlClient
Imports CommunityServer.WebServices.Membership
Imports CommunityServer.Membership

Public Class ViewAllMemberApplicants
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lbl1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents dtgNewMembers As System.Web.UI.WebControls.DataGrid
    Protected WithEvents OrgSummary As System.Web.UI.WebControls.DataGrid
    Protected WithEvents txtMemberID As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'only allow Moderators or Admins to view

        Dim oRoles As List(Of TelligentCommunity.Roles) = Session("Roles")

        If Not oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Then
            Response.Redirect("default.aspx")
        End If

        If Not IsPostBack Then
            BindDataList()
            ShowAffiliateMembershipDetails()
        End If
    End Sub

    Sub BindDataList()

        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select distinct * from members, affiliate where " & _
       "Account_Status = '" & ConfigurationManager.AppSettings.Item("MemberStatusPending") & "' " & _
       " and members.affiliate_id = affiliate.affiliate_id " & _
       "order by lastName"

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        dtgNewMembers.DataSource = dtrTitles
        dtgNewMembers.DataBind()

        dtrTitles.Close()
        conPubs.Close()
    End Sub

    Public Sub SetDropDownIndex(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ed As System.Web.UI.WebControls.DropDownList
        ed = sender
        'ed.SelectedIndex = protectedstrMemberID - 2
        '-2 because members starts at 1 not 0
        'ed.SelectedIndex = protectedstrDirectorID - 1
        'set the default dropdown index to the actual value

    End Sub
    Private Sub ShowAffiliateMembershipDetails()

        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select StatusDescription, count(1) TCount from Members, Status " & _
            "where  " & _
            "Account_Status = status_id " & _
            "group by StatusDescription;"

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        OrgSummary.DataSource = dtrTitles
        OrgSummary.DataBind()

        dtrTitles.Close()
        conPubs.Close()
    End Sub
    Public Sub dtgNewMembers_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dtgNewMembers.CancelCommand
        dtgNewMembers.EditItemIndex = -1
        'remove the edititemindex
        BindDataList()
        'refresh the grid
    End Sub

    Public Sub dtgNewMembers_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dtgNewMembers.UpdateCommand
        Dim objConn As SqlConnection = New SqlConnection((ConfigurationManager.AppSettings.Item("SQLConnectionString")))
        Dim cmdCustomers As SqlCommand
        Dim strSql, strPaidStatus As String

        strPaidStatus = CType(e.Item.FindControl("ddlMemberPaid"), DropDownList).SelectedItem.Value

        strSql = "UPDATE Members SET Paid ='" & strPaidStatus _
                & "', Date_Paid = getdate() " _
                & ", Account_Status = '" & ConfigurationManager.AppSettings.Item("MemberStatusPaid") _
                & "' WHERE Member_ID ='" & txtMemberID.Text & "'"
        cmdCustomers = New SqlCommand(strSql, objConn)
        objConn.Open()
        cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection)
        dtgNewMembers.EditItemIndex = -1
        BindDataList()
    End Sub

#Region "Create Paid Array"
    Public Function Create_Paid_Array() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add(New DataColumn("ID", GetType(String)))
        dt.Columns.Add(New DataColumn("Description", GetType(String)))

        dr = dt.NewRow()
        dr(0) = "N"
        dr(1) = "Not Paid"
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr(0) = "Y"
        dr(1) = "Paid"
        dt.Rows.Add(dr)

        Return dt
    End Function
#End Region

    Private Sub dtgNewMembers_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNewMembers.EditCommand
        dtgNewMembers.EditItemIndex = e.Item.ItemIndex
        'set details index
        'remove the counselors editindex 
        BindDataList()
        'BindTheGrid()
        'refresh grids
    End Sub

    Private Sub dtgNewMembers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNewMembers.ItemCommand

        'don't allow viewers to edit the data
        If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNoSave")) Then
            Response.Redirect("default.aspx")
        End If

        txtMemberID.Text = CType(e.Item.FindControl("lblMemberID"), Label).Text
        'protectedstrScheduleID = CType(e.Item.FindControl("lblScheduleID"), Label).Text
        'need to change the ID variables

        Select Case e.CommandName
            Case "View"

                dtgNewMembers.EditItemIndex = -1
                'remove the edit index
                BindDataList()
                'refresh counselor display

                'dtgDetails.EditItemIndex = -1
                'BindDetails()
                'refresh details display

        End Select

        dtgNewMembers.SelectedIndex = e.Item.ItemIndex
        'set the selectedindex to apply 
        'selecteditem formatting

    End Sub

End Class
